<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流程表单数据字典</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/lib/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/lib/layuiadmin/style/admin.css" media="all">
    <style>
        html, body {
            height: 100%;
        }

        .layui-fluid {
            height: 95%;
        }

        .layui-row, .layui-col-xs12, .layui-card {
            height: 96%;
        }
    </style>

</head>
<body>


<div class="layui-fluid" id="LAY-component-grid-mobile">
    <div class="layui-row layui-col-space5">
        <div class="layui-col-xs12" style="height: 10%;">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="" style="border-bottom:0;padding:0;">
                        <form class="layui-form" action="">
                            <table style="width:98%; table-layout:fixed;">
                                <tr>
                                    <td style="float: right">
                                        <a href="javascript:;" id="addBtn" class="layui-btn layui-btn-sm"
                                           onclick="edit()">
                                            <i class="layui-icon">&#xe654;</i>增加
                                            <span id="gagaga"></span>
                                        </a>
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header" style="height: 35px"><span id="title"></span></div>
                <div class="layui-card-body">
                    <table id="dataGrid" lay-filter="dataGrid"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">选择</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/static/lib/layuiadmin/layui/layui.js"></script>
<script src="../../../common/common.js"></script>
<script src="../../../common/jquery-3.3.1.min.js"></script>
<script src="../../../common/listTree.js"></script>
<script src="../../../common/dataGrid.js"></script>
<script>
    var index = parent.layer.getFrameIndex(window.name);
    var iframe = RequestURLParam.getParam("iframeId");
    var clientId = RequestURLParam.getParam("id");
    var form, laydate, table , userId;

    $(document).ready(function () {
        layui.use(['form', 'laydate', 'table'], function () {
            form = layui.form;
            laydate = layui.laydate;
            table = layui.table;
            $("#title").html("联系人列表");
            UserInfo.init();
            userId = UserInfo.userId;
            loadOrder();
        });
    });

    //加载联系人列表
    function loadOrder() {
        table.render({
            elem: '#dataGrid'
            , id: 'dataGrid'
            , height: 'full-185'
            , url: '/erp-customer-contact/select/pagebyclientId' //数据接口
            , where: {
                'clientId': clientId
            }
            , page: true //开启分页
            , method: 'post'
            , limit: 10
            , limits: [5, 10, 20, 50]
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
                , countName: 'total' //规定数据总数的字段名称，默认：count
                , dataName: 'rows' //规定数据列表的字段名称，默认：data
            }
            , cols: [[ //表头
                {type: 'numbers', title: 'ID', width: 40, fixed: 'left'}
                , {field: 'contacts', title: '联系人', width: '22%'}
                , {field: 'department', title: '部门', width: '24%'}
                , {field: 'duty', title: '职务', width: '24%'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: '24%'}
            ]]
        });
        //监听行工具事件
        table.on('tool(dataGrid)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                if(isSameDay(data.createTime)){
                    if(userId==data.createUser) {
                        removeDatahp(data.id, data.contacts);
                    }
                    else{
                        layer.confirm("该条数据不是您创建的！不可删除！");
                    }
                }else {
                    layer.confirm("该条数据不是今天创建！不可删除！");
                }
            } else if (obj.event === 'edit') {
                if(isSameDay(data.createTime)){
                    if(userId==data.createUser) {
                        edit(data.id);
                    }
                    else{
                        layer.confirm("该条数据不是您创建的！不可编辑！");
                    }
                }else {
                    layer.confirm("该条数据不是今天创建！不可编辑！");
                }
            }else if (obj.event === 'detail') {
                rootClick(data);
            }
        });
    }
//判断当前日期和创建日期是否时同一天
    function isSameDay(da1) {
        var flag = false;
        var da = new Date();
        var da2 = new Date(da1).toLocaleDateString();
        var da3 = da.toLocaleDateString();
        if(da2 == da3)flag=true;
        return flag;
    }
    
    function rootClick(e) {
        //放到 选择 按钮功能中
        node = $('#' + iframe, parent.document)[0].contentWindow.node;
        node.attr("value", e.contacts);
        node.attr("value-code", e.id);
        node.attr("value-json", JSON.stringify(e).replace(/\"/g, '\''));
        parent.layer.close(index);
    }

    //删除联系人
    function removeDatahp(id, contacts) {
        layer.confirm('确定要删除' + contacts + '么？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            url = "/erp-customer-contact/del";
            $.ajax({
                url: url,
                data: {
                    id: id
                },
                type: 'post',
                traditional: true,
                success: function () {
                    layer.msg('操作成功！', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        table.reload('dataGrid');
                    })
                }
                ,
                error: function () {
                    layer.msg('操作失败！', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                    })
                }
            });
        })
    }

    //编辑
    function edit(id) {
        var url = "";
        var title = "";
        if (id) {
            url = "/views/customer/assign/contactsEdit.html?type=editSelect&id=" + id;
            title = "修改联系人信息";
        } else {
            url = "/views/customer/assign/contactsEdit.html?type=addSelect&id=" + clientId;
            title = "添加联系人";
        }
        parent.layer.open({
            type: 2
            , title: title
            , area: ['70%', '50%']
            , id: 'tableeditlayer' //防止重复弹出
            , zIndex: parent.layer.zIndex
            , content: url
            , end: function () {
                table.reload('dataGrid');
            }
        });
        table.reload('dataGrid');
    }
</script>
</body>
</html>